/**
 * Copyright  2017 ZTE Corporation.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.openo.commontosca.inventory.sdk.api;

import java.util.NoSuchElementException;

public final class Optional<T> {
  /**
   * Common instance for {@code empty()}.
   */
  private static final Optional<?> EMPTY = new Optional<Object>();

  /**
   * If non-null, the value; if null, indicates no value is present
   */
  private final T value;

  /**
   * Constructs an empty instance.
   *
   * @implNote Generally only one empty instance, {@link Optional#EMPTY}, should exist per VM.
   */
  private Optional() {
    this.value = null;
  }

  /**
   * Constructs an instance with the value present.
   *
   * @param value the non-null value to be present
   * @throws NullPointerException if value is null
   */
  private Optional(T value) {
    if (value == null) {
      throw new NullPointerException();
    }
    this.value = value;
  }

  /**
   * Returns an empty {@code Optional} instance. No value is present for this Optional.
   *
   * @apiNote Though it may be tempting to do so, avoid testing if an object is empty by comparing
   *          with {@code ==} against instances returned by {@code Option.empty()}. There is no
   *          guarantee that it is a singleton. Instead, use {@link #isPresent()}.
   *
   * @param <T> Type of the non-existent value
   * @return an empty {@code Optional}
   */
  public static <T> Optional<T> empty() {
    @SuppressWarnings("unchecked")
    Optional<T> t = (Optional<T>) Optional.EMPTY;
    return t;
  }

  /**
   * Returns an {@code Optional} with the specified present non-null value.
   *
   * @param <T> the class of the value
   * @param value the value to be present, which must be non-null
   * @return an {@code Optional} with the value present
   * @throws NullPointerException if value is null
   */
  public static <T> Optional<T> of(T value) {
    return new Optional<T>(value);
  }

  /**
   * Returns an {@code Optional} describing the specified value, if non-null, otherwise returns an
   * empty {@code Optional}.
   *
   * @param <T> the class of the value
   * @param value the possibly-null value to describe
   * @return an {@code Optional} with a present value if the specified value is non-null, otherwise
   *         an empty {@code Optional}
   */
  public static <T> Optional<T> ofNullable(T value) {
    if (value == null) {
      return Optional.empty();
    } else {
      return Optional.of(value);
    }
  }

  /**
   * Indicates whether some other object is "equal to" this Optional. The other object is considered
   * equal if:
   * <ul>
   * <li>it is also an {@code Optional} and;
   * <li>both instances have no value present or;
   * <li>the present values are "equal to" each other via {@code equals()}.
   * </ul>
   *
   * @param obj an object to be tested for equality
   * @return {code true} if the other object is "equal to" this object otherwise {@code false}
   */
  @Override
  public boolean equals(Object obj) {
    if (this == obj) {
      return true;
    }

    if (!(obj instanceof Optional)) {
      return false;
    }

    Optional<?> other = (Optional<?>) obj;
    return (this.value == other.value) || (this.value != null && this.value.equals(other.value));
  }

  /**
   * If a value is present in this {@code Optional}, returns the value, otherwise throws
   * {@code NoSuchElementException}.
   *
   * @return the non-null value held by this {@code Optional}
   * @throws NoSuchElementException if there is no value present
   *
   * @see Optional#isPresent()
   */
  public T get() {
    if (this.value == null) {
      throw new NoSuchElementException("No value present");
    }
    return this.value;
  }

  /**
   * Returns the hash code value of the present value, if any, or 0 (zero) if no value is present.
   *
   * @return hash code value of the present value or 0 if no value is present
   */
  @Override
  public int hashCode() {
    return this.value != null ? this.value.hashCode() : 0;
  }

  /**
   * Return {@code true} if there is a value present, otherwise {@code false}.
   *
   * @return {@code true} if there is a value present, otherwise {@code false}
   */
  public boolean isPresent() {
    return this.value != null;
  }

  /**
   * Return the value if present, otherwise return {@code other}.
   *
   * @param other the value to be returned if there is no value present, may be null
   * @return the value, if present, otherwise {@code other}
   */
  public T orElse(T other) {
    return this.value != null ? this.value : other;
  }

  /**
   * Returns a non-empty string representation of this Optional suitable for debugging. The exact
   * presentation format is unspecified and may vary between implementations and versions.
   *
   * @implSpec If a value is present the result must include its string representation in the
   *           result. Empty and present Optionals must be unambiguously differentiable.
   *
   * @return the string representation of this instance
   */
  @Override
  public String toString() {
    return this.value != null ? String.format("Optional[%s]", this.value) : "Optional.empty";
  }
}
